﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for TinTucDAL
/// </summary>
public class TinTucDAL
{
	public TinTucDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable fnG_GetAllDataTinTuc()
    {
        string strSQL = "";
        strSQL = "select *,Substring(GioiThieu,0,80+CHARINDEX(' ',Substring(GioiThieu,80,DATALENGTH(GioiThieu)))) as [GioiThieu] from TinTuc_KhuyenMai order by idkm DESC";
        return DataProvider.GetData(strSQL);
    }

    public TinTucKhuyenMaiEntity fnG_GetTinTuc(string MaTinTuc)
    {
        string strSQL ="SELECT  * FROM TinTuc_KhuyenMai WHERE idkm=@Matin";
        DataTable table = DataProvider.GetData(strSQL, "@Matin", MaTinTuc);
        TinTucKhuyenMaiEntity tintuc = new TinTucKhuyenMaiEntity();
        tintuc.HinhAnhDaiDien = table.Rows[0]["HinhAnhDaiDien"].ToString();
        tintuc.Idkm = Int32.Parse(table.Rows[0]["Idkm"].ToString());
        tintuc.NgayNhap = DateTime.Parse( table.Rows[0]["NgayNhap"].ToString());
        tintuc.NoiDung = table.Rows[0]["NoiDung"].ToString();
        tintuc.TieuDe = table.Rows[0]["TieuDe"].ToString();
        tintuc.GioiThieu = table.Rows[0]["GioiThieu"].ToString();
        tintuc.Nguon = table.Rows[0]["Nguon"].ToString();
        return tintuc;
    }

    public DataTable fnG_GetTinTucNew()
    {
        string strSQL = "SELECT  * FROM TinTuc_KhuyenMai ORDER BY NgayNhap DESC ";
        return DataProvider.GetData(strSQL);
    }

    internal DataTable fnG_Get4TinTuc()
    {
        string strSQL = "";
        strSQL = "select top 4 *,Substring(GioiThieu,0,80+CHARINDEX(' ',Substring(GioiThieu,80,DATALENGTH(GioiThieu)))) as [GioiThieu1] from TinTuc_KhuyenMai order by NgayNhap DESC";
        return DataProvider.GetData(strSQL);
    }
    // cac tin tuc khac
    public DataTable fnG_GetData10TintucDiff(TinTucKhuyenMaiEntity TinTuc)
    {
        string strSQL = "";
        strSQL = "select top 10 *,'Tintuc' as TinTuc from TinTuc_KhuyenMai WHERE idkm NOT IN (select idkm from TinTuc_KhuyenMai where idkm=@idkm)";
        return DataProvider.GetData(strSQL, "@idkm", TinTuc.Idkm);
    }
    public int fnG_InsertTinTucKhuyenMai(TinTucKhuyenMaiEntity TinTuc)
    {
        string sql = "INSERT INTO TinTuc_KhuyenMai VALUES(@TieuDe,@GioiThieu ,@NgayNhap,@NoiDung,@HinhAnhDaiDien,@Nguon)";
        return DataProvider.ExecuteNonQuery(sql, "@TieuDe", TinTuc.TieuDe, "@GioiThieu", TinTuc.GioiThieu, "@NgayNhap", TinTuc.NgayNhap, "@NoiDung", TinTuc.NoiDung, "@HinhAnhDaiDien", TinTuc.HinhAnhDaiDien, "@Nguon", TinTuc.Nguon);
    }
    //update tin tuc
    public int fnG_UpdateTinTucKhuyenMai(TinTucKhuyenMaiEntity TinTuc)
    {
        string sql = "Update TinTuc_KhuyenMai Set TieuDe=@TieuDe,GioiThieu=@GioiThieu ,NgayNhap=@NgayNhap,NoiDung=@NoiDung,HinhAnhDaiDien=@HinhAnhDaiDien,Nguon=@Nguon where idkm=@idkm";
        return DataProvider.ExecuteNonQuery(sql, "@TieuDe", TinTuc.TieuDe, "@GioiThieu", TinTuc.GioiThieu, "@NgayNhap", TinTuc.NgayNhap, "@NoiDung", TinTuc.NoiDung, "@HinhAnhDaiDien", TinTuc.HinhAnhDaiDien, "@Nguon", TinTuc.Nguon,"@idkm",TinTuc.Idkm);
    }
    //delete
    public int fnG_DeleteTinTuc(TinTucKhuyenMaiEntity TinTuc)
    {
        string sql = "delete from TinTuc_KhuyenMai where idkm=@idkm";
        return DataProvider.ExecuteNonQuery(sql, "@idkm", TinTuc.Idkm);

    }
    //active
    public int fnG_ActiveTinTucKhuyenMai(TinTucKhuyenMaiEntity TinTuc)
    {
        string sql = "Update TinTuc_KhuyenMai Set NgayNhap=@NgayNhap where idkm=@idkm";
        return DataProvider.ExecuteNonQuery(sql, "@NgayNhap",DateTime.Now,"@idkm", TinTuc.Idkm);
    }
    public DataTable fnG_GetDataForDate(DateTime tungay, DateTime denngay)
    {
        string sql = "select * from TinTuc_KhuyenMai where datediff(day,@tungay,NgayNhap)>=0 and datediff(day,NgayNhap,@denngay)>=0";
        return DataProvider.GetData(sql, "@tungay", tungay, "@denngay", denngay);
    }
    public DataTable fnG_TimKiemTintuc(string timkiem)
    {
        string sql = "select * from TinTuc_KhuyenMai where (select dbo.funcNVarchar(TieuDe)) like (select dbo.funcNVarchar(@TieuDe));";
        return DataProvider.GetData(sql, "@Tieude", timkiem);
    }
}